package com.giaothong;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.giaothong.entities.Question;
import com.giaothong.entities.Score;
import com.giaothong.entities.Setting;
import com.giaothong.entities.Topic;

public class TopicCoverActivity extends Activity implements OnClickListener{
	public static final int PASS_SCORE_XEMOTO = 12; 
	public static final int PASS_SCORE_XEOTO = 26;
	public static final int PASS_SCORE_XEKHACH = 28;
	public static final int PASS_SCORE_XETAI = 28;
	
	public static final int DURATION_XEOTO = 15;
	public static final int DURATION_XEMOTO = 25;
	public static final int DURATION_XEKHACH = 20;
	public static final int DURATION_XETAI = 20;
	
	public static int topicID;
	public static String topicTitle = "";
	public static int duration;
	public static int passScore;
	
	private TextView txtNameTopic, txtNumberOfQues, txtDuration, txtPassRequied;
	private LinearLayout layoutParent;
	private Button btnHome, btnStart;
	Setting setting;
	
	public static EditText textUserName;
	
	final private String TAG = "TopicCoverActivity";
	
	public static Collection<Question> questionList = new ArrayList<Question>();
	public static Collection<Question> questionListBuffer; // list dem


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_topic_cover);
		Log.i("TopicCoverActivity", "onCreate");
		unit();
		setting = Main.getSettingDAL().getSetting();
		
		// lay thong tin Topic
		Bundle intent = getIntent().getExtras();
		int topicId = intent.getInt("topic_id");
		int examId = intent.getInt("examId");
		
		getListQuestion(examId,topicId);
		setValueToText(topicId); 
		
				
}//end onCreate()
	
	private void getListQuestion(int idExam, int idTopic){
		if (idExam == 0) {
			// tao bo de thi MOI (new) duoc lay ngau nhien theo Cau hinh so
			// luong cau hoi
			// questionList = Main.getQuestionDAL().getQuestions();
			switch(idTopic){
			case 1: //xe moto
				topicID = 1;
				duration = DURATION_XEOTO;
				passScore = PASS_SCORE_XEMOTO;
				
				topicTitle = getResources().getString(R.string.topic_cover_topicXeMoto_Details);
				topicTitle = getResources().getString(R.string.topic_cover_topicXeMoto_Details);
				questionListBuffer = Main.getQuestionDAL().getQuestionsByTopic(1,7);
				for(Question question1 : questionListBuffer)
					questionList.add(question1);
				questionListBuffer.clear();
				
				questionListBuffer = Main.getQuestionDAL().getQuestionsByTopic(7,5);
				for(Question question7 : questionListBuffer)
					questionList.add(question7);
				questionListBuffer.clear();
				
				questionListBuffer = Main.getQuestionDAL().getQuestionsByTopic(8,3);
				for(Question question8 : questionListBuffer)
					questionList.add(question8);
				questionListBuffer.clear();
				//sau khi da add vao questionList, lam rong cac list buffer
				break;
			case 2: // xe o to
				topicID = 2;
				duration = DURATION_XEOTO;
				passScore = PASS_SCORE_XEOTO;
				topicTitle = getResources().getString(R.string.topic_cover_topic4Banh_Details);
				break;
			case 3: // xe khach
				topicID = 3;
				duration = DURATION_XEKHACH;
				passScore = PASS_SCORE_XEKHACH;
				
				topicTitle = getResources().getString(R.string.topic_cover_topic4Banh_Details);
				break;
			case 4: // xe tai
				topicID = 4;
				duration = DURATION_XETAI;
				passScore = PASS_SCORE_XETAI;
				
				topicTitle = getResources().getString(R.string.topic_cover_topic4Banh_Details);
				break;
			}	
		} else {
			// lay bo de thi tu history
//			questionList = Main.getExamHistoryDAL().getExamByID(examId)
//					.getQuestionList();
		}
	}
	private void unit(){
		txtNameTopic = (TextView) findViewById(R.id.topic_cover_txtname_topic);
		txtNumberOfQues = (TextView)findViewById(R.id.topic_cover_txtnumberCauHoi);
		txtPassRequied = (TextView) findViewById(R.id.topic_cover_txtscore_required);
		txtDuration = (TextView) findViewById(R.id.topic_cover_text_duration);
		
		btnHome = (Button) findViewById(R.id.topic_cover_ibtn_home);
		btnStart = (Button) findViewById(R.id.topic_cover_btn_begin);
		textUserName = (EditText) findViewById(R.id.topic_cover_value_username);
		layoutParent = (LinearLayout) findViewById(R.id.topic_relative);
		
		//textUserName.setBackgroundColor(Color.BLACK);
		
		btnHome.setOnClickListener(this);
		btnStart.setOnClickListener(this);
		textUserName.setOnClickListener(this);
		layoutParent.setOnClickListener(this);
		
		// retrieve UI components 
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}
	
	private void setValueToText(int idTopic){
		
		// set values
		switch(idTopic){
		case 1:
			txtNameTopic.setText(getResources().getString(R.string.topic)+"  "+getResources().getString(R.string.topic_cover_topicXeMoto_Details));
			txtNumberOfQues.setText(getResources().getString(R.string.topic_cover_numberCauHoi) + " 15 cau");
			txtPassRequied.setText(getResources().getString(R.string.topic_cover_diemDau) + PASS_SCORE_XEMOTO + "/15 cau");
			txtDuration.setText(getResources().getString(R.string.topic_cover_duration) + DURATION_XEMOTO +" phut");
			break; 
		case 2:
			txtNameTopic.setText(getResources().getString(R.string.topic)+" "+getResources().getString(R.string.topic_cover_topic4Banh_Details));
			txtNumberOfQues.setText(getResources().getString(R.string.topic_cover_numberCauHoi)+"  30");
			txtPassRequied.setText(getResources().getString(R.string.topic_cover_diemDau) + PASS_SCORE_XEOTO +"/30 cau");
			txtDuration.setText(getResources().getString(R.string.topic_cover_duration)+ DURATION_XEOTO +" phut");
			break;
		case 3:
			txtNameTopic.setText(getResources().getString(R.string.topic)+" "+getResources().getString(R.string.topic_cover_topic4Banh_Details));
			txtNumberOfQues.setText(getResources().getString(R.string.topic_cover_numberCauHoi)+"  30");
			txtPassRequied.setText(getResources().getString(R.string.topic_cover_diemDau) + PASS_SCORE_XEKHACH +"/30 cau");
			txtDuration.setText(getResources().getString(R.string.topic_cover_duration)+ DURATION_XEKHACH +" phut");
			break;
		case 4:
			txtNameTopic.setText(getResources().getString(R.string.topic)+" "+getResources().getString(R.string.topic_cover_topic4Banh_Details));
			txtNumberOfQues.setText(getResources().getString(R.string.topic_cover_numberCauHoi)+"  30");
			txtPassRequied.setText(getResources().getString(R.string.topic_cover_diemDau) + PASS_SCORE_XETAI +"/30 cau");
			txtDuration.setText(getResources().getString(R.string.topic_cover_duration)+ DURATION_XETAI +" phut");
			break;
		}
		
		// Lay Score Last de hien thi ten nhap sau nhat
		List<Score> scoreLast = Main.getScoreDAL().getScoreHistory();
		if (scoreLast.size() > 0)
			textUserName.setText("" + scoreLast.get(0).getUsername());
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.topic_cover_ibtn_home:
			questionList.clear();
			
			TopicCoverActivity.this.finish();
			break;
		case R.id.topic_cover_btn_begin:
			Intent itent = new Intent(TopicCoverActivity.this,QuestionTest.class);
			startActivity(itent);
			TopicCoverActivity.this.finish();
			break;
		case R.id.topic_cover_value_username:
			textUserName.setCursorVisible(true);
			textUserName.setBackgroundResource(R.color.gray_black);
			textUserName.setTextColor(Color.WHITE);
			break;
		case R.id.topic_relative:
			textUserName.setCursorVisible(false);
			textUserName.setBackgroundColor(Color.WHITE);
			textUserName.setTextColor(Color.BLACK);
			offKeyBoard();
			break;
		}
		
	}
	public void offKeyBoard() {
		layoutParent.requestFocus();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(layoutParent.getWindowToken(), 0);
	}
	@Override
	public void onBackPressed() {
		TopicCoverActivity.this.finish();
	}
}
